package com.big.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.big.cart.CartFeignClient;
import com.big.merchandise.MerchandiseFeignClient;
import com.big.model.model.merchandise.MerchandiseDetail;
import com.big.model.model.merchandise.MerchandiseInfo;
import com.big.model.model.order.MerchandiseOrder;
import com.big.model.model.record.MerchandiseOrderRecord;
import com.big.model.vo.cart.RedisCartVo;
import com.big.order.mapper.MerchandiseOrderRecordMapper;
import com.big.order.service.OrderRecordService;
import com.big.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
@Slf4j
@Transactional(rollbackFor=Exception.class)
public class OrderRecordServiceImpl extends ServiceImpl<MerchandiseOrderRecordMapper, MerchandiseOrderRecord> implements OrderRecordService {


     @Autowired
     OrderService orderService;

     @Autowired
     CartFeignClient cartFeignClient;

     @Autowired
     MerchandiseFeignClient merchandiseFeignClient;
     /**
     * 订单记录商品
     * @return
     */
    @Override
    public boolean saveMerchandiseOrderRecord(List<MerchandiseOrderRecord> merchandiseOrderRecords) {
        //TODO 当把商品记录完毕,执行删除redis购物车缓存
        System.out.println(merchandiseOrderRecords);
        String oId = merchandiseOrderRecords.get(0).getOId();//订单编号
        MerchandiseOrder merchandiseOrder = orderService.infoMerchandiseOrder(oId);
        merchandiseOrderRecords.forEach(item->{
            //写入数据库记录
            baseMapper.insert(item);
            //执行删除购物车操作
            RedisCartVo redisCartVo = new RedisCartVo();
            redisCartVo.setUid(String.valueOf(merchandiseOrder.getPId()));
            redisCartVo.setMid(String.valueOf(item.getMId()));
            redisCartVo.setSpackage(String.valueOf(item.getPackId()));
            cartFeignClient.deCartMerchandise(redisCartVo);
        });
        return true;
    }

    /**
     * 订单商品记录列表
     * @param oId
     * @return
     */
    @Override
    public List<MerchandiseOrderRecord> infoMerchandiseOrderRecords(String oId) {
        QueryWrapper<MerchandiseOrderRecord> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(oId)){
              queryWrapper.eq("o_id",oId);
        }
        List<MerchandiseOrderRecord> merchandiseOrderRecordList = baseMapper.selectList(queryWrapper);
        //删除缓存
        merchandiseOrderRecordList.forEach(item -> {
           this.MerchandiseOrderRecordInfoMerchandise(item);
        });

        return  merchandiseOrderRecordList;
    }

    /**
     * 订单商品描述
     */
    public MerchandiseOrderRecord MerchandiseOrderRecordInfoMerchandise(MerchandiseOrderRecord merchandiseOrderRecord){
        MerchandiseDetail merchandiseDetail = merchandiseFeignClient.FindMerchanInfo(String.valueOf(merchandiseOrderRecord.getMId()));
        MerchandiseInfo infoMerchandise = merchandiseFeignClient.findInfoMerchandise(String.valueOf(merchandiseOrderRecord.getMId()));
        merchandiseOrderRecord.getParam().put("image",merchandiseDetail.getDImage());
        merchandiseOrderRecord.getParam().put("name",infoMerchandise.getName());
        return  merchandiseOrderRecord;
    }

}
